package com.ruoyi.crm.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.domain.bo.CrmProductInfoBo;
import com.ruoyi.crm.domain.vo.CrmProductInfoVo;
import com.ruoyi.crm.domain.CrmProductInfo;
import com.ruoyi.crm.mapper.CrmProductInfoMapper;
import com.ruoyi.crm.service.ICrmProductInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 产品管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-10
 */
@RequiredArgsConstructor
@Service
public class CrmProductInfoServiceImpl implements ICrmProductInfoService {

    private final CrmProductInfoMapper baseMapper;

    /**
     * 查询产品管理
     */
    @Override
    public CrmProductInfoVo queryById(Long productId){
        return baseMapper.selectVoById(productId);
    }

    /**
     * 查询产品管理列表
     */
    @Override
    public TableDataInfo<CrmProductInfoVo> queryPageList(CrmProductInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CrmProductInfo> lqw = buildQueryWrapper(bo);
        Page<CrmProductInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);

    }

    /**
     * 查询产品管理列表
     */
    @Override
    public List<CrmProductInfoVo> queryList(CrmProductInfoBo bo) {
        LambdaQueryWrapper<CrmProductInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CrmProductInfo> buildQueryWrapper(CrmProductInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CrmProductInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, CrmProductInfo::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), CrmProductInfo::getProductName, bo.getProductName());
        lqw.eq(bo.getPrice() != null, CrmProductInfo::getPrice, bo.getPrice());
        lqw.eq(bo.getCostPrice() != null, CrmProductInfo::getCostPrice, bo.getCostPrice());
        lqw.eq(bo.getAggregate() != null, CrmProductInfo::getAggregate, bo.getAggregate());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), CrmProductInfo::getProductNumber, bo.getProductNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getProductDescription()), CrmProductInfo::getProductDescription, bo.getProductDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getProductUnit()), CrmProductInfo::getProductUnit, bo.getProductUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getLaunch()), CrmProductInfo::getLaunch, bo.getLaunch());
        return lqw;
    }

    /**
     * 新增产品管理
     */
    @Override
    public Boolean insertByBo(CrmProductInfoBo bo) {
        CrmProductInfo add = BeanUtil.toBean(bo, CrmProductInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProductId(add.getProductId());
        }
        return flag;
    }

    /**
     * 修改产品管理
     */
    @Override
    public Boolean updateByBo(CrmProductInfoBo bo) {
        CrmProductInfo update = BeanUtil.toBean(bo, CrmProductInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CrmProductInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除产品管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
